package com.simplegovern.setting.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import com.simpleframe.web.action.SimpleframeAction;
import com.simplegovern.setting.FormHtmlBulider;
import com.simplegovern.setting.Setting;
import com.simplegovern.setting.SettingGroup;
import com.simplegovern.setting.service.SettingGroupService;
import com.simplegovern.setting.service.SettingService;

@Scope("prototype")
@Namespace("/setting")
@Action("/setting/setting")
@Results( {   
    @Result(name = "main", location = "main.jsp"),
    @Result(name = "tomain", location = "${ctx}/setting/setting!main.shtml?settingGroup.id=${setting.group}", type="redirect")
})
public class SettingAction extends SimpleframeAction {

	@Autowired
	private SettingService settingService;
	@Autowired
	private SettingGroupService settingGroupService;
	private List<Setting> settingList;
	private List<SettingGroup> settingGroupList;
	private SettingGroup settingGroup;
	private Setting setting;
	private String formHtml;
	
	/**
	 * 显示配置列表，并显示需要编辑的配置
	 * @return
	 * @throws Exception
	 */
	public String main()throws Exception{
		if(settingGroup==null){
			settingGroup = new SettingGroup();
			settingGroup.setId(new Long(1));
		}
		//得到配置分组
		settingGroup = settingGroupService.getSettingGroupByID(settingGroup.getId());
		//得到配置分组列表
		settingGroupList = settingGroupService.getSettingGroupList();
		//得到分组下的配置列表
		settingList = settingService.getSettingList(settingGroup.getId());
		//得到当前要显示编辑的配置
		if(setting!=null && setting.getId()!=null){
			setting = settingService.getSettingByID(setting.getId());
			FormHtmlBulider fhb = new FormHtmlBulider(setting);
			formHtml = fhb.getOption();
		}
		
		return MAIN;
	}
	
	/**
	 * 需要编辑配置的表单
	 * @return
	 */
	public String getFormHtml(){
		return formHtml;
	}
	
	/**
	 * 更新配置
	 * @return
	 * @throws Exception
	 */
	public String update()throws Exception{
		System.out.println("===update");
		Setting update = settingService.getSettingByID(setting.getId());
		update.setValue(setting.getValue());
		settingService.updateSetting(update);
		setting = update;//用户返回列表时用
		addMessage("系统配置项设置成功");
		//定义一个观察者的消息
		
		return "tomain";
	}

	public Setting getSetting() {
		return setting;
	}

	public void setSetting(Setting setting) {
		this.setting = setting;
	}

	public List<Setting> getSettingList() {
		return settingList;
	}
	
	public List<SettingGroup> getSettingGroupList() {
		return settingGroupList;
	}
	
	public void setSettingGroup(SettingGroup settingGroup) {
		this.settingGroup = settingGroup;
	}
	
	public SettingGroup getSettingGroup() {
		return settingGroup;
	}
	

}
